//
//  AccountParser.m
//  Bespoke
//
//  Created by Cheng on 12/7/16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "GDataXMLNode.h"
#import "AccountParser.h"
#import "AccountInfo.h"
#import "MediaListsMedia.h"
#import "AccountInitialization.h"
#import "Split.h"

@implementation AccountParser

+ (NSMutableArray *)loadAccountInfo:(NSData *)data {
    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    
    
    //Create the MediaLists Master Array Container
    NSMutableArray *AccountArray = [[NSMutableArray alloc] init];
    
    
    //Get the Media Memebers from the XML
    NSArray *AccountInfoarray = [doc nodesForXPath:@"//AccountInfo" error:nil];
    
    //Loop into each Media Memeber
    for (GDataXMLElement * AccountElement in AccountInfoarray)
    {
        
        //Initialize the Media comopnent
        NSString *AccountUserName = [[AccountElement attributeForName:@"UserName"] stringValue];
        NSString *AccountLevel = [[AccountElement attributeForName:@"Level"] stringValue];
        NSString *AccountName = [[AccountElement attributeForName:@"Name"] stringValue];
        NSString *Organization = [[AccountElement attributeForName:@"Organization"] stringValue];
        NSString *NS_Channel = [[AccountElement attributeForName:@"Channel"] stringValue];
        NSString *NS_WeddingDate = [[AccountElement attributeForName:@"WeddingDate"] stringValue];
        int Enable = [[[AccountElement attributeForName:@"Enable"] stringValue] intValue];
        
        
        //Create and initialize one MediaOB
        AccountInfo *AccountOB = [[AccountInfo alloc] init:AccountUserName SetLevel:AccountLevel
                                               SetFullName:AccountName SetOrganization:Organization
                                                SetChannel:NS_Channel SetWeddingDate:NS_WeddingDate
                                                 SetEnable:Enable];
        
        NSArray *ShareGroupElements = [AccountElement elementsForName:@"ShareGroup"];
        
        AccountShareGroup *ShareGroupOB = [[AccountShareGroup alloc] init];
        
        AccountOB.ShareGroup = ShareGroupOB;
        
        NSArray *GroupElements = [[ShareGroupElements objectAtIndex:0] elementsForName:@"Group"];
        for(GDataXMLElement *GroupElement in GroupElements)
        {
            int Group_ID = [[[GroupElement attributeForName:@"ID"] stringValue] intValue];
            NSString *NS_GroupName = [[GroupElement attributeForName:@"Name"]stringValue];
            
            AccountGroup *GroupOB = [[AccountGroup alloc]ID:Group_ID Name:NS_GroupName];
            
            
            NSArray *UserElements = [GroupElement elementsForName:@"User"];
            for(GDataXMLElement *UserElement in UserElements)
            {
                int UserID = [[[UserElement attributeForName:@"ID"]stringValue] intValue];
                NSString *NS_UserName = [[UserElement attributeForName:@"Name"]stringValue];
                NSString *NS_UserEmail = [[UserElement attributeForName:@"Email"]stringValue];
                
                AccountGroupUser *GroupUserOB = [[AccountGroupUser alloc]UserID:UserID name:NS_UserName email:NS_UserEmail];
                
                [GroupOB.UserArray addObject:GroupUserOB];
            }
            [ShareGroupOB.GroupArray addObject:GroupOB];
        }
        
        //Create MyBoard object
        NSArray *MyBoardElements = [AccountElement elementsForName:@"MyBoard"];
        NSString *MediaDown_loadPath = [[[MyBoardElements objectAtIndex:0] attributeForName:@"MediaDownloadPath"]stringValue];
        
        //Create and load the MyBoard Object
        AccountMyBoard *MyBoardOB = [[AccountMyBoard alloc]MediaDownloadPath:MediaDown_loadPath];
        
        AccountOB.MyBoard = MyBoardOB;
        
        //Create Board object
        NSArray *BoardElements = [[MyBoardElements objectAtIndex:0] elementsForName:@"Board"];
        for(GDataXMLElement *BoardElement in BoardElements)
        {
            int Board_ID = [[[BoardElement attributeForName:@"ID"] stringValue] intValue];
            NSString *BoardName = [[BoardElement attributeForName:@"Name"]stringValue];
            NSString *BoardShareGroup = [[BoardElement attributeForName:@"ShareGroup"]stringValue];
        
            //Create and load the Board Object
            AccountBoard *BoardOB = [[AccountBoard alloc]BoardID:Board_ID
                                                            name:BoardName
                                                      ShareGroup:BoardShareGroup];
            
            
            //Create Media object
            NSArray *MediaElements = [BoardElement elementsForName:@"Media"];
            for(GDataXMLElement *MediaElement in MediaElements)
            {
                //Create and initialize one MediaOB
                MediaListsMedia *MediaOB = [[MediaListsMedia alloc] init];
                
                //Initialize the Media comopnent
                int MediaID = [[[MediaElement attributeForName:@"ID"] stringValue] intValue];
                NSString *NS_BeMediaID = [[MediaElement attributeForName:@"BeMediaID"] stringValue];
                NSString *MediaType = [[MediaElement attributeForName:@"MediaType"] stringValue];
                NSString *WebLink = [[[MediaElement attributeForName:@"WebLink"] stringValue] StringParseSymbol];
                NSString *NS_Source = [[MediaElement attributeForName:@"Source"] stringValue];
                NSString *NS_Author = [[MediaElement attributeForName:@"Author"] stringValue];
                NSString *NS_lastUpdate = [[MediaElement attributeForName:@"lastUpdate"] stringValue];
                
                //Assign The Media Object
                MediaOB.MediaID = MediaID;
                MediaOB.NS_BeMediaID = NS_BeMediaID;
                MediaOB.MediaType = MediaType;
                MediaOB.WebLink = WebLink;
                MediaOB.NS_Source = NS_Source;
                MediaOB.NS_Author = NS_Author;
                MediaOB.NS_lastUpdate = NS_lastUpdate;
                
                //Create MediaText object
                NSArray *TextsElements = [MediaElement elementsForName:@"Text"];
                int text_ID = [[[[TextsElements objectAtIndex:0] attributeForName:@"ID"]stringValue]intValue];
                NSString *titleTxt = [[[[TextsElements objectAtIndex:0] attributeForName:@"Title"]stringValue] StringParseSymbol];
                NSString *descriptionTxt = [[[[TextsElements objectAtIndex:0] attributeForName:@"Description"]stringValue] StringParseSymbol];
                
                //Create and load the Text Object
                MediaListsText *TextOB = [[MediaListsText alloc]InitialWithtext_id:text_ID 
                                                                             title:titleTxt 
                                                                       description:descriptionTxt];
                
                MediaOB.text = TextOB;
                
                
                NSArray *Videos = [MediaElement elementsForName:@"Video"];
                int video_ID = [[[[Videos objectAtIndex:0] attributeForName:@"ID"] stringValue] intValue];
                NSString *video_Type = [[[Videos objectAtIndex:0] attributeForName:@"Type"] stringValue];
                NSString *fileName = [[[[Videos objectAtIndex:0] attributeForName:@"FileName"] stringValue] StringParseSymbol];
                //Create and load the Video Object
                MediaListsVideo *VideoOB =[[MediaListsVideo alloc] video_id:video_ID
                                                                       type:video_Type
                                                                   filename:fileName];
                MediaOB.video = VideoOB;
                
                //Initialize the Image Array within the Object
                MediaOB.imageArray = [[NSMutableArray alloc] init];
                //Load into each of Image Element
                NSArray *ImageElements = [MediaElement elementsForName:@"Image"];
                for(GDataXMLElement *imageElement in ImageElements)
                {
                    int imageID = [[[imageElement attributeForName:@"ID"] stringValue] intValue];
                    NSString *imageType = [[imageElement attributeForName:@"Type"] stringValue];
                    int Width = [[[imageElement attributeForName:@"Width"] stringValue] intValue];
                    int Height = [[[imageElement attributeForName:@"Height"] stringValue] intValue];
                    NSString *fileName = [[[imageElement attributeForName:@"FileName"] stringValue] StringParseSymbol];
                    //Create and Load the Image Object
                    MediaListsImage *ImageOB =[[MediaListsImage alloc] image_id:imageID
                                                                           type:imageType
                                                                       filename:fileName Width:Width Height:Height];
                    //Add the ImageOB into the image Array of MediaOB    
                    [MediaOB.imageArray addObject:ImageOB];
                }
                
                
                
                
                
                //Initialize the Comment Array within the Object
                MediaOB.Array_Comment = [[NSMutableArray alloc] init];
                
                //Load into each of Comment Element
                NSArray *CommentElements = [MediaElement elementsForName:@"Comment"];
                
                for(GDataXMLElement *CommentElement in CommentElements)
                {
                    int CommentID = [[[CommentElement attributeForName:@"ID"] stringValue] intValue];
                    NSString *NS_Content = [[[CommentElement attributeForName:@"Content"] stringValue] StringParseSymbol];
                    NSString *NS_UserName = [[CommentElement attributeForName:@"User"] stringValue];
                    NSString *NS_Date = [[CommentElement attributeForName:@"lastUpdate"] stringValue];
                    
                    //NSString Convert NSDate.
                    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
                    [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
                    
                    //Create and Load the Comment Object
                    MediaListsComment *CommentOB =[[MediaListsComment alloc] init:CommentID SetContent:NS_Content SetUserName:NS_UserName SetDate:[formatter dateFromString:NS_Date]];
                    //Add the CommentOB into the Comment Array of MediaOB
                    [MediaOB.Array_Comment addObject:CommentOB];
                }
                
                
                //Initialize the Note Array within the Object
                MediaOB.Array_Notes = [[NSMutableArray alloc] init];
                
                //Load into each of Note Element
                NSArray *NoteElements = [MediaElement elementsForName:@"Note"];
                
                for(GDataXMLElement *NoteElement in NoteElements)
                {
                    int NoteID = [[[NoteElement attributeForName:@"ID"] stringValue] intValue];
                    NSString *NoteText = [[[NoteElement attributeForName:@"Text"] stringValue] StringParseSymbol];
                    int AxisX = [[[NoteElement attributeForName:@"AxisX"] stringValue] intValue];
                    int AxisY = [[[NoteElement attributeForName:@"AxisY"] stringValue] intValue];
                    
                    MediaListsNote *NoteOB = [[MediaListsNote alloc] init:NoteID SetNoteText:NoteText SetAxisX:AxisX SetAxisY:AxisY];
                    
                    [MediaOB.Array_Notes addObject:NoteOB];
                }
                
                
                NSArray *Array_Html = [MediaElement elementsForName:@"beHtml"];
                NSString *NS_ImageName = [[[Array_Html objectAtIndex:0] attributeForName:@"ThumbnailImage"] stringValue];
                int Width = [[[[Array_Html objectAtIndex:0] attributeForName:@"ThumbnailWidth"] stringValue] intValue];
                int Height = [[[[Array_Html objectAtIndex:0] attributeForName:@"ThumbnailHeight"] stringValue] intValue];
                NSString *NS_HTMLtitle = [[[[Array_Html objectAtIndex:0] attributeForName:@"Title"] stringValue] StringParseSymbol];
                NSString *NS_HTMLDescription = [[[[Array_Html objectAtIndex:0] attributeForName:@"Description"] stringValue] StringParseSymbol];
                
                //Create and load the Html Object
                MediaListsHtml *HtmlOB =[[MediaListsHtml alloc] InitialWithHtmlImageName:NS_ImageName SetImageWidth:Width
                                                                          SetImageHeight:Height SetHtmlTitle:NS_HTMLtitle SetHtmlDescription:NS_HTMLDescription];
                MediaOB.MLH_html = HtmlOB;
                
                

                [BoardOB.MediaArray addObject:MediaOB];
            }
            [MyBoardOB.BoardArray addObject:BoardOB];
        }
        
        //Parse Initialization
        NSArray *Initialization = [AccountElement elementsForName:@"Initialization"];
        AccountInitialization *InitializationOB = [[AccountInitialization alloc]init];
        AccountOB.Initialization = InitializationOB;
        
        
        //Parse Categories
        /**NSArray *Array_Channels = [[Initialization objectAtIndex:0] elementsForName:@"Channel"];
        for (GDataXMLElement *ChannelElement in Array_Channels)
        {
            int ChannelID = [[[ChannelElement attributeForName:@"ID"] stringValue] intValue];
            NSString *NS_ChannelName = [[ChannelElement attributeForName:@"Name"] stringValue];
            AccountChannel *ChannelOB =[[AccountChannel alloc] InitChannel:ChannelID SetChannelName:NS_ChannelName];
            
            
            NSArray *Categories = [ChannelElement elementsForName:@"Categories"];
            NSArray *CategoryElements = [[Categories objectAtIndex:0] elementsForName:@"Category"];
            
            for (GDataXMLElement *CategoryElement in CategoryElements)
            {
                int CategoryID = [[[CategoryElement attributeForName:@"ID"] stringValue] intValue];
                NSString *NS_CategoryName = [[CategoryElement attributeForName:@"Name"] stringValue];
                AccountCategory *CategoryOB =[[AccountCategory alloc] InitCategory:CategoryID SetCategoryName:NS_CategoryName];
                
                //Add Categories
                [ChannelOB.Array_Categories addObject:CategoryOB];
            }
            
            //Add Channels
            [InitializationOB.Array_Channels addObject:ChannelOB];
        }**/
        
        //Parse Colors
        NSArray *Colors = [[Initialization objectAtIndex:0] elementsForName:@"Colors"];
        NSArray *ColorElements = [[Colors objectAtIndex:0] elementsForName:@"Color"];
        for (GDataXMLElement *ColorElement in ColorElements)
        {
            NSString *ColorName = [[ColorElement attributeForName:@"Name"] stringValue];
            NSString *ColorHexValue = [[ColorElement attributeForName:@"HexValue"] stringValue];
            AccountColor *ColorOB =[[AccountColor alloc] Color_Name:ColorName 
                                                     Color_HexValue:ColorHexValue];
            //Add Colors
            [InitializationOB.ColorArray addObject:ColorOB];
        }
        
        //Parse Tags
        NSArray *Tags = [[Initialization objectAtIndex:0] elementsForName:@"Tags"];
        NSArray *TagElements = [[Tags objectAtIndex:0] elementsForName:@"Tags"];
        for (GDataXMLElement *TagElement in TagElements)
        {
            NSString *TagName = [[TagElement attributeForName:@"Name"] stringValue];
            int Tagsize = [[[TagElement attributeForName:@"Size"] stringValue] intValue];
            AccountTag *TagOB =[[AccountTag alloc] Tag_Name:TagName 
                                                   Tag_size:Tagsize];
            //Add Tags
            [InitializationOB.TagArray addObject:TagOB];
        }
        
        //Load into each of RecommendedFeed Element
        NSArray *ReFeedElements = [AccountElement elementsForName:@"RecommendedFeed"];
        
        for(GDataXMLElement *ReFeedElement in ReFeedElements)
        {
            NSString *Title = [[[ReFeedElement attributeForName:@"Title"] stringValue] StringParseSymbol];
            NSString *Content = [[[ReFeedElement attributeForName:@"Content"] stringValue] StringParseSymbol];
            NSString *Link = [[[ReFeedElement attributeForName:@"Link"] stringValue] StringParseSymbol];
            
            //Create and Load the ReFeed Object
            AccountRecommendedFeed *ReFeedOB =[[AccountRecommendedFeed alloc] init:Title SetContent:Content SetLink:Link];
            
            //Add the ReFeedOB into the ReFeed Array of Account
            [AccountOB.Array_ReFeed addObject:ReFeedOB];
        }
        
        //Parser Feed.
        AccountFeed *FeedOB = [[AccountFeed alloc] init];
        
        NSArray *Array_Feed = [AccountElement elementsForName:@"Feed"];
        
        NSArray *FB_Elements = [[Array_Feed objectAtIndex:0] elementsForName:@"Facebook"];
        NSString *NS_FB_Id = [[[FB_Elements objectAtIndex:0] attributeForName:@"ID"]stringValue];
        NSString *NS_FB_Token = [[[FB_Elements objectAtIndex:0] attributeForName:@"Token"]stringValue];
        int FB_Switch = [[[[FB_Elements objectAtIndex:0] attributeForName:@"Switch"]stringValue]intValue];
        
        FeedOB.Ob_Facebook = [[AccountFacebook alloc] init:NS_FB_Id setFacebookToken:NS_FB_Token setFacebookSwitch:FB_Switch];
    
        NSArray *TW_Elements = [[Array_Feed objectAtIndex:0] elementsForName:@"Twitter"];
        NSString *NS_TW_Key = [[[TW_Elements objectAtIndex:0] attributeForName:@"Key"]stringValue];
        NSString *NS_TW_Secret = [[[TW_Elements objectAtIndex:0] attributeForName:@"Secret"]stringValue];
        int TW_Switch = [[[[TW_Elements objectAtIndex:0] attributeForName:@"Switch"]stringValue]intValue];
        
        FeedOB.Ob_Twitter = [[AccountTwitter alloc]init:NS_TW_Key setTwitterSecret:NS_TW_Secret setTwitterSwitch:TW_Switch];
        
        NSArray *TB_Elements = [[Array_Feed objectAtIndex:0] elementsForName:@"Tumblr"];
        NSString *NS_TB_BlogName = [[[TB_Elements objectAtIndex:0] attributeForName:@"Name"]stringValue];
        int TB_Switch = [[[[TB_Elements objectAtIndex:0] attributeForName:@"Switch"]stringValue]intValue];
        
        FeedOB.Ob_Tumblr = [[AccountTumblr alloc]init:NS_TB_BlogName setTumblrSwitch:TB_Switch];
        
        NSArray *IG_Elements = [[Array_Feed objectAtIndex:0] elementsForName:@"Instagram"];
        NSString *NS_IG_Token = [[[IG_Elements objectAtIndex:0] attributeForName:@"Token"]stringValue];
        int IG_Switch = [[[[IG_Elements objectAtIndex:0] attributeForName:@"Switch"]stringValue]intValue];
        
        FeedOB.Ob_Instagram = [[AccountInstagram alloc] init:NS_IG_Token setInstagramSwitch:IG_Switch];
        
        NSArray *GoogleFeed_Elements = [[Array_Feed objectAtIndex:0] elementsForName:@"GoogleFeed"];
        
        for (GDataXMLElement *GoogleFeed_Element  in GoogleFeed_Elements) {
            
            NSString *NS_Google_Title = [[GoogleFeed_Element attributeForName:@"Title"] stringValue];
            NSString *NS_Google_Content = [[GoogleFeed_Element attributeForName:@"Content"] stringValue];
            NSString *NS_Google_Link = [[GoogleFeed_Element attributeForName:@"Link"] stringValue];
            int Google_Switch = [[[GoogleFeed_Element attributeForName:@"Switch"] stringValue] intValue];
            
            AccountGoogleFeed *GoogleFeedOB =[[AccountGoogleFeed alloc] init:NS_Google_Title
                                                                  SetContent:NS_Google_Content
                                                                     SetLink:NS_Google_Link
                                                              SetGooglSwitch:Google_Switch];
            [FeedOB.Array_GoogleFeed addObject:GoogleFeedOB];
        }
        
        AccountOB.Feed = FeedOB;
    
        [AccountArray addObject:AccountOB];
        
    }

    return AccountArray;
}

@end
